package Tools;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 李天航
 * 日期运算的工具类
 */
public class DateCompute {
    //2020.1.6 为基准(第一个星期一)

    /**
     * 当前是第几周
     * @return 距离2020.1.6的周数
     */
    /**
     * 1~12月的天数
     */
    private static int[] monthDays=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};

    /**
     * 判断是不是闰年
     * @param year 年份
     * @return true:是 false:否
     */
    public static boolean isSpecialYear(int year)
    {
        if((year%4==0&&year%100!=0)||year%400==0) return true;
        return false;
    }


    public static String getNowTime()
    {
        Date d=new Date();
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        String nowDateTime = df.format(d);
        return nowDateTime;
    }
    /**
     * 从2020.1.6开始是第几天
     * @return 天数
     */
    public static int getDaysFrom20200106()
    {
        Date d=new Date();
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        String nowDateTime = df.format(d);
        int speYear=0;
        int nowYear=Integer.parseInt(nowDateTime.substring(0,4));
        int nowMonth=Integer.parseInt(nowDateTime.substring(5,7));
        int nowDay=Integer.parseInt(nowDateTime.substring(8,10));
        for(int i=2020;i<nowYear;i++)
        {
            if(isSpecialYear(i))
            {
                speYear++;
            }
        }
        //2020.1.6
        int days=(nowYear-2020)*365+speYear-5;
        for(int i=1;i<nowMonth;i++)
        {
            days+=monthDays[i];
        }
        days+=nowDay;
        return days;
    }

    /**
     * 当前是第几周
     * @return 周数
     */
    public static int getNowWeekNum()
    {
        int days=getDaysFrom20200106();
        return days/7+1;
    }

    /**
     * 当前星期几
     * @return 星期几
     */
    public static int getWeekIdx()
    {
        return getDaysFrom20200106()%7;
    }


    /**
     * 返回从2020.1.6的分钟数
     * @return
     */
    public static long getNowMinutesFrom20200106()
    {
        long allminutes=getDaysFrom20200106()*24*60;
        Date d=new Date();
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String nowDateTime = df.format(d);
        int hours=Integer.parseInt(nowDateTime.substring(11,13));
        int minutes=Integer.parseInt(nowDateTime.substring(14,16));
        allminutes+=hours*24+minutes;
        return allminutes;
    }
    public static long getNowMinutesByWeekNumAndMinutes(long weekNum,long minutes)
    {
        return (weekNum-1)*7*24*60+minutes;
    }
    public static void main(String[] args)
    {
        System.out.println(getNowMinutesFrom20200106());
    }
}
